package com.meituan.service.impl;

import com.meituan.dao.PermissionRepository;
import com.meituan.dao.RolePermissionMapper;
import com.meituan.dao.RolePermissionRepository;
import com.meituan.dao.RoleRepository;
import com.meituan.pojo.entity.Permission;
import com.meituan.pojo.entity.Role;
import com.meituan.pojo.entity.RolePermission;
import com.meituan.pojo.req.RolePermissionReq;
import com.meituan.pojo.req.RoleReq;
import com.meituan.pojo.resp.BasePermissionResp;
import com.meituan.pojo.resp.BasePermissionValueResp;
import com.meituan.pojo.resp.BaseResp;
import com.meituan.service.RoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    RoleRepository roleRepository;
    @Autowired
    PermissionRepository permissionRepository;
    @Autowired
    RolePermissionRepository rolePermissionRepository;
    @Autowired
    RolePermissionMapper rolePermissionMapper;

    @Override
    public BaseResp findAll() {
        List<Role> all = roleRepository.findAll();
        return new BaseResp(1,"success",all);
    }

    @Override
    public BaseResp deleteById(Integer rid) {
        roleRepository.deleteById(rid);
        return new BaseResp().SUCCESS();
    }

    @Override
    public BaseResp saveOrUpdate(RoleReq roleReq) {
        Role role = new Role();
        BeanUtils.copyProperties(roleReq,role);
        roleRepository.saveAndFlush(role);
        return new BaseResp().SUCCESS();
    }

    @Override
    public BaseResp findPermissionByRid(Integer rid) {
        List<Permission> all = permissionRepository.findAll();
        List allPerList = new ArrayList();
        for (Permission permission:all){
            BasePermissionResp basePermissionResp = new BasePermissionResp();
            basePermissionResp.setKey(permission.getPid());
            basePermissionResp.setLabel(permission.getPermissionName());
            basePermissionResp.setDisable(false);
            allPerList.add(basePermissionResp);
        }
        List<Integer> ids = permissionRepository.selectPidByRid(rid);
        BasePermissionValueResp basePermissionValueResp = new BasePermissionValueResp();
        basePermissionValueResp.setData(allPerList);
        basePermissionValueResp.setValue(ids);
        return new BaseResp(1,"success",basePermissionValueResp);
    }

    @Override
    public BaseResp updateRolePermission(RolePermissionReq rolePermissionReq) {
        Integer rid = rolePermissionReq.getRid();
        List<Integer> pidList = rolePermissionReq.getPidList();
        List<RolePermission> byRid = rolePermissionRepository.findByRid(rid);
        if (byRid!=null){
            //说明该角色有权限，删掉该角色的所有的权限
            rolePermissionMapper.deletePermission(rid);
        }
        //建立新的权限逻辑 使用mybatis的批量新增 foreach的使用
        if (pidList!=null&&pidList.size()>0){
            rolePermissionMapper.addRolePermission(rolePermissionReq);
        }

        return new BaseResp().SUCCESS();
    }
}
